clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

****************************************************************************************************
********************************** Figure 2: The effects of temperature on poverty and inequality
****************************************************************************************************

// Country-level
* FE model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

collapse (mean) poor215 poor365 poor685 gini theil temp rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech, by(code year)

estimates clear
qui reghdfe poor215 temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	est store fe215
qui reghdfe poor365 temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	est store fe365
qui reghdfe poor685 temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	est store fe685
qui reghdfe gini temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	est store fegini
qui reghdfe theil temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	est store fetheil
	
* Long difference model
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   

bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2

encode geo_code2_new, gen(id_region)
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech {
	gen `v'_d = D.`v'
}

collapse (mean) poor215_d poor365_d poor685_d gini_d theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d edupri_d eduter_d popgr_d tech_d, by(code year)
	
rename 	temp_d temp
qui reg poor215_d temp rainfall_sum_d humid_d `controls', cluster(code) 	
	est store ld215
qui reg poor365_d temp rainfall_sum_d humid_d `controls', cluster(code) 	
	est store ld365
qui reg poor685_d temp rainfall_sum_d humid_d `controls', cluster(code) 	
	est store ld685
qui reg gini_d temp rainfall_sum_d humid_d `controls', cluster(code) 	
	est store ldgini
qui reg theil_d temp rainfall_sum_d humid_d `controls', cluster(code) 	
	est store ldtheil	
		
coefplot (fe215 ld215) (fe365 ld365)  (fe685 ld685) ///
		, keep(temp) legend(off) graphregion(color(white)) plotregion(color(white)) ///
		aseq swapnames xline(0, lpattern(dash) lcolor(black)) mcolor(navy) ciopts(lcol(navy)) ///
		coeflabels(fe215 = "Fixed-effects" fe365 = "Fixed-effects" fe685 = "Fixed-effects" ///
		ld215 = "Long-differences" ld365 = "Long-differences" ld685 = "Long-differences", labsize(small)) ///
		groups(fe215 ld215 = "$2.15/day" fe365 ld365 = "$3.65/day" fe685 ld685 = "$6.85/day", labsize(small)) ///
		xtitle(Coefficient estimates of temperature) title(Country-level analysis) ///
		mlabel(cond(@pval<.01, string(@b,"%9.3f")+"***", ///
		cond(@pval<.05, string(@b,"%9.3f")+"**", cond(@pval<.1, string(@b,"%9.3f")+"*", string(@b,"%9.3f"))))) ///
		mlabpos(1) mlabcolor(navy) mlabsize() xlabel(-3(1)3) name("gr1", replace)
	
coefplot (fegini ldgini) (fetheil ldtheil)  ///
		, keep(temp) legend(off) graphregion(color(white)) plotregion(color(white)) ///
		aseq swapnames xline(0, lpattern(dash) lcolor(black)) mcolor(navy) ciopts(lcol(navy)) ///
		coeflabels(fegini = "Fixed-effects" fetheil = "Fixed-effects" ///
		ldgini = "Long-differences" ldtheil = "Long-differences" , labsize(small)) ///
		groups(fegini ldgini = "Gini" fetheil ldtheil = "Theil", labsize(small)) ///
		xtitle(Coefficient estimates of temperature) title(Country-level analysis) ///
		mlabel(cond(@pval<.01, string(@b,"%9.3f")+"***", ///
		cond(@pval<.05, string(@b,"%9.3f")+"**", cond(@pval<.1, string(@b,"%9.3f")+"*", string(@b,"%9.3f"))))) ///
		mlabpos(1) mlabcolor(navy) mlabsize() xlabel(-4(2)4) name("gr2", replace)
	
// subnational-level

* FE model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

estimates clear
qui reghdfe poor215 temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store fe215
qui reghdfe poor365 temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store fe365
qui reghdfe poor685 temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store fe685
qui reghdfe gini temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store fegini
qui reghdfe theil temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	est store fetheil
	
* Long difference model
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   

bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech {
	gen `v'_d = D.`v'
}

collapse (mean) poor215_d poor365_d poor685_d gini_d theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d edupri_d eduter_d popgr_d tech_d, by(geo_code2_new year)

rename 	temp_d temp
qui reg poor215_d temp rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 	
	est store ld215
qui reg poor365_d temp rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 	
	est store ld365
qui reg poor685_d temp rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 	
	est store ld685
qui reg gini_d temp rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 	
	est store ldgini
qui reg theil_d temp rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 	
	est store ldtheil
	
coefplot (fe215 ld215) (fe365 ld365)  (fe685 ld685) ///
		, keep(temp) legend(off) graphregion(color(white)) plotregion(color(white)) ///
		aseq swapnames xline(0, lpattern(dash) lcolor(black)) mcolor(navy) ciopts(lcol(navy)) ///
		coeflabels(fe215 = "Fixed-effects" fe365 = "Fixed-effects" fe685 = "Fixed-effects" ///
		ld215 = "Long-differences" ld365 = "Long-differences" ld685 = "Long-differences", labsize(small)) ///
		groups(fe215 ld215 = "$2.15/day" fe365 ld365 = "$3.65/day" fe685 ld685 = "$6.85/day", labsize(small)) ///
		xtitle(Coefficient estimates of temperature) title(Subnational-level analysis) ///
		mlabel(cond(@pval<.01, string(@b,"%9.3f")+"***", ///
		cond(@pval<.05, string(@b,"%9.3f")+"**", cond(@pval<.1, string(@b,"%9.3f")+"*", string(@b,"%9.3f"))))) ///
		mlabpos(1) mlabcolor(navy) mlabsize() xlabel(-3(1)3) name("gr3", replace)
		
coefplot (fegini ldgini) (fetheil ldtheil)  ///
		, keep(temp) legend(off) graphregion(color(white)) plotregion(color(white)) ///
		aseq swapnames xline(0, lpattern(dash) lcolor(black)) mcolor(navy) ciopts(lcol(navy)) ///
		coeflabels(fegini = "Fixed-effects" fetheil = "Fixed-effects" ///
		ldgini = "Long-differences" ldtheil = "Long-differences" , labsize(small)) ///
		groups(fegini ldgini = "Gini" fetheil ldtheil = "Theil", labsize(small)) ///
		xtitle(Coefficient estimates of temperature) title(Subnational-level analysis) ///
		mlabel(cond(@pval<.01, string(@b,"%9.3f")+"***", ///
		cond(@pval<.05, string(@b,"%9.3f")+"**", cond(@pval<.1, string(@b,"%9.3f")+"*", string(@b,"%9.3f"))))) ///
		mlabpos(1) mlabcolor(navy) mlabsize() xlabel(-4(2)4) name("gr4", replace)
	
	
gr combine gr1 gr3, graphregion(fcolor(white) lcolor(white) lwidth(none) ilwidth(none) margin(zero) ) xsize(8)
graph export "${date}/_figures/_fig_2a.pdf", as(pdf) replace			
	
gr combine gr2 gr4, graphregion(fcolor(white) lcolor(white) lwidth(none) ilwidth(none) margin(zero) ) xsize(8)
graph export "${date}/_figures/_fig_2b.pdf", as(pdf) replace			
		
	
			